<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="visible" :close-on-click-modal="false">
		<el-form ref="form" :model="form" :rules="rules">
			<el-form-item label="压缩包名称" :label-width="formLabelWidth" prop="name">
				<el-input v-model="form.name" autocomplete="off" placeholder="请输入压缩包名称"></el-input>
			</el-form-item>
			<el-form-item label="压缩路径" :label-width="formLabelWidth" prop="url">
				<fortress-upload type="file" name="url" :limit="1" :size="100" :formData="form"></fortress-upload>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="visible = false" size="medium" type="danger">取 消</el-button>
			<el-button type="primary" size="medium" @click="submitForm('form')">确 定</el-button>
		</div>
	</el-dialog>
</template>
<script>
import { cmstransfertagsInfo, cmstransfertagsSave, cmstransfertagsEdit } from "@/api/cms/cmstransfertags.js"
import {} from "@/api/public.api.js"

export default {
	data() {
		return {
			visible: false,
			form: {
				name: "",
				url: "",
			},
			formLabelWidth: "120px",
			rules: {
				url: [{ required: true, message: "请上传一个文件", trigger: "blur" }],
				name: [{ required: true, message: "请输入文件名称", trigger: "blur" }],
			},
			statusList: [],
		}
	},
	created() {},
	watch: {
		"form.url"(val) {
			if (val) {
				if (val.length > 0) {
					let urlJson = []
					val.forEach(item => {
						urlJson.push({ name: item.name, url: item.url })
					})
					if (!this.form.name && urlJson.length > 0) {
						this.form.name = urlJson[0].name
					}
				}
			}
		},
	},
	methods: {
		queryJsonList() {
			this.statusList = []
			let statusListData = '[{"code":"0","name":"转换中"},{"code":"1","name":"已完成"}]'
			this.statusList = JSON.parse(statusListData)
		},
		init(id) {
			this.resetForm()
			this.queryJsonList()
			//修改
			if (id) {
				cmstransfertagsInfo(id).then(resp => {
					if (resp.code == 0) {
						this.$nextTick(() => {
							this.form = {
								...this.form,
								...resp.data,
							}
							if (resp.data.url) {
								this.form.url = []
								let urlArray = JSON.parse(resp.data.url)
								urlArray.forEach(item => {
									this.form.url.push({ name: item.name, url: item.url })
								})
							}
						})
					} else {
						this.$msg({
							message: resp.msg,
							type: "error",
						})
					}
				})
			}
			this.visible = true
		},
		//提交表单数据
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.url) {
						let urlJson = []
						this.form.url.forEach(item => {
							urlJson.push({ name: item.name, url: item.url })
						})
						this.form.url = JSON.stringify(urlJson)
					}
					//修改
					if (this.form.id) {
						cmstransfertagsEdit(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
						//新增
					} else {
						cmstransfertagsSave(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
					}
				} else {
					console.log("error submit!!")
					return false
				}
			})
		},
		resetForm() {
			this.form = {
				name: "",
				url: "",
			}
			this.$nextTick(() => {
				if (this.$refs["form"]) {
					this.$refs["form"].resetFields()
				}
			})
		},
	},
}
</script>
<style scoped>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}
.avatar-uploader .el-upload:hover {
	border-color: #409eff;
}
.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
}
.avatar {
	width: 178px;
	height: 178px;
	display: block;
}
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
	height: auto;
	max-height: 274px;
	padding: 7px;
	overflow: hidden;
	overflow-y: auto;
	font-weight: normal;
}
</style>
